<template>
  <div class="goods">
    <HeaderNav></HeaderNav> 
    <div v-if="loading" class="loading">加载中...</div>
    <div v-else v-for="(items, category) in categorizedGoods" :key="category" class="category">
      <h2>{{ category }}</h2>
      <ul>
        <li v-for="item in items" :key="item.gid" class="goods-item">
          <!-- 商品类型 -->
          <span class="goods-category">{{ item.cgname }}</span>
          <!-- 商品图片 -->
          <div class="goods-image">
            <img :src="`../../public/images/${item.img}`" :alt="item.gname" />
          </div>
          <!-- 商品信息 -->
          <div class="goods-info">
            <span class="goods-id">商品ID: {{ item.gid }}</span>
            <span class="goods-name">商品名称: {{ item.gname }}</span>
          </div>
          <!-- 查看详情链接 -->
          <router-link :to="`/goodsdetail?goodsid=${item.gid}`" class="detail-link">查看详情</router-link>
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup>
import HeaderNav from '@/components/common/HeaderNav.vue'
import axios from '../api/axios';
import { useRoute } from 'vue-router';
import { ref, computed } from 'vue';

const route = useRoute();
const goodsList = ref([]);
const loading = ref(true);
let skey = route.query.skey;

axios.post('/api/goods/all', { skey: skey }).then((res) => {
    // 如果地址栏没有查询关键字，直接返回所有商品
    goodsList.value = res.data.goods;
    console.log(goodsList.value)
    loading.value = false;
  })
  .catch(() => {
    loading.value = false;
  });

// 根据商品类型对商品进行分类
const categorizedGoods = computed(() => {
  return goodsList.value.reduce((acc, item) => {
    if (!acc[item.cgname]) {
      acc[item.cgname] = [];
    }
    acc[item.cgname].push(item);
    return acc;
  }, {});
});
</script>

<style lang="scss" scoped>
.goods {
  padding: 30px;
  background-color: #f8f8f8;
  min-height: 100vh;
  font-family: "PingFang SC", "Helvetica Neue", Arial, sans-serif;

  .loading {
    text-align: center;
    font-size: 18px;
    color: #666;
    padding: 40px;
    display: flex;
    flex-direction: column;
    align-items: center;
    
    &::before {
      content: "";
      display: block;
      width: 40px;
      height: 40px;
      border: 3px solid rgba(255, 103, 0, 0.2);
      border-top-color: #ff6700;
      border-radius: 50%;
      animation: spin 1s linear infinite;
      margin-bottom: 20px;
    }
  }

  @keyframes spin {
    to { transform: rotate(360deg); }
  }

  .category {
    margin-bottom: 40px;
    max-width: 1200px;
    margin-left: auto;
    margin-right: auto;

    h2 {
      font-size: 26px;
      color: #333;
      padding: 15px 0;
      margin-bottom: 20px;
      position: relative;
      font-weight: 500;
      
      &::after {
        content: "";
        position: absolute;
        bottom: 0;
        left: 0;
        width: 60px;
        height: 3px;
        background-color: #ff6700;
        border-radius: 3px;
      }
    }

    ul {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
      gap: 20px;
      padding: 0;

      .goods-item {
        background-color: white;
        border-radius: 12px;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
        margin-bottom: 0;
        padding: 20px;
        display: flex;
        flex-direction: column;
        transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
        position: relative;
        overflow: hidden;

        &:hover {
          transform: translateY(-5px);
          box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
        }

        .goods-category {
          font-size: 14px;
          color: #ff6700;
          background-color: rgba(255, 103, 0, 0.1);
          padding: 4px 10px;
          border-radius: 12px;
          margin-bottom: 15px;
          align-self: flex-start;
        }

        .goods-image {
          width: 100%;
          height: 180px;
          margin-right: 0;
          margin-bottom: 15px;
          overflow: hidden;
          border-radius: 8px;
          position: relative;
          
          img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            transition: transform 0.5s ease;
          }
          
          &::after {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(to bottom, rgba(255,255,255,0) 80%, rgba(255,255,255,0.9) 100%);
          }
        }

        .goods-info {
          flex-grow: 1;
          display: flex;
          flex-direction: column;
          margin-bottom: 15px;

          .goods-id {
            font-size: 13px;
            color: #999;
            margin-bottom: 5px;
          }

          .goods-name {
            font-size: 17px;
            color: #333;
            margin: 0;
            font-weight: 500;
            line-height: 1.4;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }

        .detail-link {
          text-decoration: none;
          color: white;
          font-size: 15px;
          padding: 10px 20px;
          border-radius: 20px;
          background-color: #ff6700;
          transition: all 0.3s ease;
          text-align: center;
          display: inline-block;
          border: none;
          position: relative;
          overflow: hidden;
          
          &::before {
            content: "";
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: 0.5s;
          }
          
          &:hover {
            background-color: #e65c00;
            box-shadow: 0 4px 12px rgba(255, 103, 0, 0.3);
            
            &::before {
              left: 100%;
            }
          }
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .goods {
    padding: 15px;
    
    .category {
      h2 {
        font-size: 22px;
      }
      
      ul {
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
        gap: 15px;
        
        .goods-item {
          padding: 15px;
          
          .goods-image {
            height: 140px;
          }
          
          .detail-link {
            padding: 8px 15px;
            font-size: 14px;
          }
        }
      }
    }
  }
}
</style>
